package com.lc.common.utils;

/**
 * 图片转webp格式
 * https://blog.csdn.net/gmaaa123/article/details/134084845
 */


import cn.hutool.core.img.Img;
import com.luciad.imageio.webp.WebPReadParam;
import com.luciad.imageio.webp.WebPWriteParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CompressImgUtils {

    private static final Logger log = LoggerFactory.getLogger(CompressImgUtils.class);

    /**
     * webp格式图片转成JPG格式
     *
     * @param sourcePath 源文件路径
     * @param targetPath 目标文件路径
     */
    private static File webp2jpg(String sourcePath, String targetPath) {
        // 创建WebP ImageReader实例
        ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();
        // 配置解码参数
        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);
        // 在ImageReader设置读取的原文件
        File file = null;
        try {
            File sourceFile = new File(sourcePath);
            FileImageInputStream fileImageInputStream = new FileImageInputStream(sourceFile);
            reader.setInput(fileImageInputStream);
            // 解码图像
            BufferedImage image = reader.read(0, readParam);
            // 设置输入文件的格式和文件名
            file = new File(targetPath);
            ImageIO.write(image, "jpg", file); // 这里也可以使用其他图片格式，但是格式和文件名后缀要保持一致
            fileImageInputStream.flush();
            fileImageInputStream.close();
        } catch (IOException e) {
            log.error("webp转jpg异常:" + sourcePath + "-" + targetPath, e);
        }
        return file;
    }

    /**
     * 图片转成webp格式
     *
     * @param sourcePath 源文件路径
     * @param targetPath 目标文件路径
     * @return
     */
    public static File img2webp(String sourcePath, String targetPath) {
        File file = null;
        try {
            File sourceFile = new File(sourcePath);
            // 获取原始文件的编码
            BufferedImage image = ImageIO.read(sourceFile);
            // 创建WebP ImageWriter实例
            ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
            // 配置编码参数
            WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
            // 设置压缩模式
            writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);
            // 配置ImageWriter输出
            file = new File(targetPath);
            FileImageOutputStream outputStream = new FileImageOutputStream(file);
            writer.setOutput(outputStream);
            // 进行编码，重新生成新图片
            writer.write(null, new IIOImage(image, null, null), writeParam);
            outputStream.flush();
            outputStream.close();
            // 删除源文件
            //FileUtil.del(sourceFile);
        } catch (Exception e) {
            log.error("图片转webp异常:" + sourcePath + "-" + targetPath, e);
        }
        return file;
    }


    public static File img2jpg(String sourcePath, String targetPath) {
        File targetFile = null;
        try {
            File sourceFile = new File(sourcePath);
            targetFile = new File(targetPath);
            long fileSize = sourceFile.length();
            double quality;
            if (fileSize > 1024 * 1024) {
                quality = 0.5;
            } else if (fileSize > 512 * 1024) {
                quality = 0.6;
            } else if (fileSize > 256 * 1024) {
                quality = 0.8;
            } else {
                quality = 1;
            }
            Img.from(sourceFile)
                    .setQuality(quality)//压缩比率
                    .write(targetFile);
        } catch (Exception e) {
            log.error("图片转jpg异常:" + sourcePath + "-" + targetPath, e);
        }
        return targetFile;
    }


}